// pages/inspectionCompany/inspectionCompany.js
//index.js 
//获取应用实例 
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalList:[],
    dataList:[],
    name:'江苏',
    canShow:false,
    indexVersion:'',
    canShowlbmtName:false,
    TypeInfo:'全部',
    sortStr:'',
    lbmtNumber:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getHospitalInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
    //记录安装师傅点击问题汇总和巡检汇总的操作
    recordInstallersOptions(){
      var that = this
      lbbService.recordInstallersOptions({
        province: that.data.name?that.data.name:'',
        city: that.data.ctname?that.data.ctname:''
      },function(res){
        that.getCityList()
      })
    },
  // 获取城市
  getCity(e){
    this.setData({
      name:e.detail,
      ctname:''
    })
    this.recordInstallersOptions()

  },
   // 获取城市
   getCt(e) {
    this.setData({
      ctname: e.detail,
    })
    this.recordInstallersOptions()

  },
  getHospitalInfo(e) {
    var that = this;
    lbbService.getSimWarningList({}, function (res) {
      var cityArr= []
      that.setData({
        hospitalList: res.data
      })
      that.data.hospitalList.forEach((item)=>{
        cityArr.push(item.province)
      })
      var cityArray = new Set(cityArr)
      var arrA = [...cityArray]
      var resultArray = arrA.sort(
        function (param1, param2) {
            return param1.localeCompare(param2,"zh");
        }
    );
      that.setData({
        cityArray:resultArray
      })
      that.recordInstallersOptions()
    })
  },
  // 获取城市的list
  getCityList(){
    var that = this
    var smallCityArr = []
    that.data.dataList = that.data.hospitalList.filter((item)=>{
      return item.province == that.data.name
    })
    that.setData({
      dataList:that.data.dataList
    })
     // 新增城市列表
     that.data.cityDataList = that.data.dataList.filter((item) => {
      smallCityArr.push(item.city)
    })
    var ctarray = new Set(smallCityArr)
    console.log('ctarray', ctarray)
    // var nproblemArr = new Set(problemArr)
    var arrA = [...ctarray]
    console.log('arrA', arrA)
    // var nproblemArrA = [...nproblemArr]
    var resultctArray = arrA.sort(
      function (param1, param2) {
        return param1.localeCompare(param2, "zh");
      }
    );
    that.setData({
      ctArray: resultctArray,
      // ctname: that.data.ctname? that.data.ctname:resultctArray[0],
    })
    if (that.data.ctname) {
      // 筛选出 市
      that.data.dataList = that.data.dataList.filter((item) => {
        return item.city == that.data.ctname
      })
    }
    that.setData({
      dataList:that.data.dataList
    })
    //  ------------------------------------------------------------------
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 跳转到医院详情
  jumpInfo(e) {
    wx.navigateTo({
      url: '../hospitalDetail/hospitalDetail?hospitalId=' + e.currentTarget.dataset.id,
    })
  },
  selectTap(){
    this.setData({
      canShow:!this.data.canShow
    })
    if(this.data.canShowlbmtName){
      this.setData({
        canShowlbmtName:false
      })
    }
  },
  selectLbmTap(){
    this.setData({
      canShowlbmtName:!this.data.canShowlbmtName
    })
    if(this.data.canShow){
      this.setData({
        canShow:false
      })
    }
  },
  optionVersionTap(e){
    var that = this
    let Index=e.currentTarget.dataset.index;
    let sortStr=e.currentTarget.dataset.str;
    console.log(Index,e)
    that.setData({
      indexVersion:Index,
      sortStr:sortStr,
      canShow:!that.data.canShow
     });
     that.getHospitalInfo()
  },
  // getSortType(e){
  //   var that = this;
  //   var data = ''
  //   var name = ''
  //   if(e == 'sort'){
  //     name = 'sortType'
  //     data = that.data.indexVersion
  //   }else {
  //     name = 'filterStr'
  //     data = that.data.TypeInfo
  //   }
  //   lbbService.getHospitalInspectionSummary({[name]:data}, function (res) {
  //     var cityArr= []
  //     that.setData({
  //       hospitalList: res.data.inspectionSummarys,
  //       sortMap:res.data.sortMap,
  //       indexVersion:res.data.sortMap.earliest,
  //       mechanismTypeInfos:res.data.mechanismTypeInfos,
  //       TypeInfo:res.data.mechanismTypeInfos[0].lbmtName,
  //     })
  //     that.data.hospitalList.forEach((item)=>{
  //       cityArr.push(item.province)
  //     })
  //     var cityArray = new Set(cityArr)
  //     var arrA = [...cityArray]
  //     that.setData({
  //       cityArray:arrA
  //     })
  //     that.getCityList()
  //   })
  // },
  optionlbmTap(e){
    var that = this
    let Index=e.currentTarget.dataset.index;
    let lbmtNumber=e.currentTarget.dataset.number
    that.setData({
      TypeInfo:Index,
      lbmtNumber:lbmtNumber,
      canShowlbmtName:!that.data.canShowlbmtName
     });
     that.getHospitalInfo()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})